package com.igeek.d0612.test2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: zhiyu
 * @Date:16:30
 * @DESC:
 */
public class AccountLock {
    private String name;
    private Double balance;
    private final Lock lock = new ReentrantLock();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public AccountLock(String name, Double balance) {
        this.name = name;
        this.balance = balance;
    }
    public AccountLock withdraw(double money){
        lock.lock();
        try {
            if (money>this.balance){
                System.out.println("余额不足,"+this.toString());
            }else {
                this.setBalance(this.getBalance()-money);
                System.out.println("取款成功,"+this.toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return this;
    }

    @Override
    public String toString() {
        return "Account{" +
                "账户名='" + name + '\'' +
                ", 余额=" + balance +
                '}';
    }
}
